home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Devil's Cubes 1.0.1 / source / Devil’s Cubes ƒ / Cube code / cube.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.5 KB  |  269 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        cube.c
  4.  
  5. Purpose:    This module handles game initialization & shutdown, and
  6.             handles key and mouse events during a game.
  7.  
  8.  
  9. Devil’s Cubes -- a simple cubes puzzle
  10. Copyright (C) 1993 Mark Pilgrim
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "cube.h"
  30. #include "cube meat.h"
  31. #include "cube generic open.h"
  32. #include "cube load-save.h"
  33. #include "cube files.h"
  34. #include "cube graphics.h"
  35. #include "cube win.h"
  36. #include "msg graphics.h"
  37. #include "msg menus.h"
  38. #include "msg environment.h"
  39. #include "msg sounds.h"
  40.  
  41. PicHandle        gCleanMirror;
  42. PicHandle        gKeyPic;
  43.  
  44. Boolean            gUseMirror;
  45. Boolean            gShowAll;
  46. int                Cube[4][6];
  47.  
  48. PixPatHandle    gColorPatterns[4];
  49. Handle            gBWPatterns[4];
  50.  
  51. void InitGame(void)
  52. {
  53.     int            i,j,k;
  54.     AppFile        myFile;
  55.     
  56.     gWindowHeight=300;
  57.     gWindowWidth=500;
  58.     
  59.     gCleanMirror=GetPicture(201);
  60.     gKeyPic=GetPicture(202);
  61.     
  62.     for (i=0; i<4; i++)
  63.     {
  64.         if (gHasColorQD)
  65.             gColorPatterns[i]=GetPixPat(128+i);
  66.         gBWPatterns[i]=GetPattern(128+i);
  67.     }
  68.     
  69.     gameFile.name[0]=0x00;
  70.     deleteTheThing=FALSE;
  71.     
  72.     CountAppFiles(&i, &j);
  73.     if ((j>0) && (i==0))
  74.     {
  75.         GetAppFiles(1, &myFile);
  76.         MyMakeFSSpec(myFile.vRefNum, 0, myFile.fName, &gameFile);
  77.         GenericOpen(&gameFile);
  78.         for (k=1; k<=j; k++)
  79.             ClrAppFiles(k);
  80.     }
  81. }
  82.  
  83. void NewGame(void)
  84. {
  85.     int            i,r,d;
  86.     
  87.     Cube[0][0]=Cube[0][3]=Cube[1][1]=Cube[2][4]=Cube[3][4]=0;
  88.     Cube[0][1]=Cube[1][0]=Cube[2][2]=Cube[2][5]=Cube[3][0]=Cube[3][2]=Cube[3][3]=1;
  89.     Cube[0][4]=Cube[0][5]=Cube[1][2]=Cube[1][4]=Cube[2][3]=Cube[3][1]=2;
  90.     Cube[0][2]=Cube[1][3]=Cube[1][5]=Cube[2][0]=Cube[2][1]=Cube[3][5]=3;
  91.     
  92.     for (i=0; i<100; i++)
  93.     {
  94.         r=(Random()&0x7fff)%4;
  95.         d=(Random()&0x7fff)%6;
  96.         switch (d)
  97.         {
  98.             case 0:
  99.                 Qrotate(r);
  100.                 break;
  101.             case 1:
  102.                 Arotate(r);
  103.                 break;
  104.             case 2:
  105.                 Zrotate(r);
  106.                 break;
  107.             case 3:
  108.                 Wrotate(r);
  109.                 break;
  110.             case 4:
  111.                 Srotate(r);
  112.                 break;
  113.             case 5:
  114.                 Xrotate(r);
  115.                 break;
  116.         }
  117.     }
  118.     
  119.     OpenMainWindow();
  120.     AdjustMenus();
  121. }
  122.  
  123. void GameUndo(void)
  124. {
  125. }
  126.  
  127. void GameEvent(void)
  128. {
  129. }
  130.  
  131. void GameKeyEvent(char charPressed)
  132. {
  133.     int            doUpdate;
  134.     
  135.     ObscureCursor();
  136.     
  137.     doUpdate=-1;
  138.     
  139.     if (charPressed>0x60)
  140.         charPressed-=0x20;
  141.         
  142.     switch (charPressed)
  143.     {
  144.         case 'Q':
  145.             Qrotate(0);
  146.             doUpdate=0;
  147.             break;
  148.         case 'A':
  149.             Arotate(0);
  150.             doUpdate=0;
  151.             break;
  152.         case 'Z':
  153.             Zrotate(0);
  154.             doUpdate=0;
  155.             break;
  156.         case 'W':
  157.             Wrotate(0);
  158.             doUpdate=0;
  159.             break;
  160.         case 'S':
  161.             Srotate(0);
  162.             doUpdate=0;
  163.             break;
  164.         case 'X':
  165.             Xrotate(0);
  166.             doUpdate=0;
  167.             break;
  168.         case 'E':
  169.             Qrotate(1);
  170.             doUpdate=1;
  171.             break;
  172.         case 'D':
  173.             Arotate(1);
  174.             doUpdate=1;
  175.             break;
  176.         case 'C':
  177.             Zrotate(1);
  178.             doUpdate=1;
  179.             break;
  180.         case 'R':
  181.             Wrotate(1);
  182.             doUpdate=1;
  183.             break;
  184.         case 'F':
  185.             Srotate(1);
  186.             doUpdate=1;
  187.             break;
  188.         case 'V':
  189.             Xrotate(1);
  190.             doUpdate=1;
  191.             break;
  192.         case 'T':
  193.             Qrotate(2);
  194.             doUpdate=2;
  195.             break;
  196.         case 'G':
  197.             Arotate(2);
  198.             doUpdate=2;
  199.             break;
  200.         case 'B':
  201.             Zrotate(2);
  202.             doUpdate=2;
  203.             break;
  204.         case 'Y':
  205.             Wrotate(2);
  206.             doUpdate=2;
  207.             break;
  208.         case 'H':
  209.             Srotate(2);
  210.             doUpdate=2;
  211.             break;
  212.         case 'N':
  213.             Xrotate(2);
  214.             doUpdate=2;
  215.             break;
  216.         case 'U':
  217.             Qrotate(3);
  218.             doUpdate=3;
  219.             break;
  220.         case 'J':
  221.             Arotate(3);
  222.             doUpdate=3;
  223.             break;
  224.         case 'M':
  225.             Zrotate(3);
  226.             doUpdate=3;
  227.             break;
  228.         case 'I':
  229.             Wrotate(3);
  230.             doUpdate=3;
  231.             break;
  232.         case 'K':
  233.             Srotate(3);
  234.             doUpdate=3;
  235.             break;
  236.         case ',':
  237.             Xrotate(3);
  238.             doUpdate=3;
  239.             break;
  240.     }
  241.  
  242.     if (doUpdate>=0)
  243.     {
  244.         DoSound(sound_rotate);
  245.         if (GetWindowDepth() <= 2)
  246.             DrawCubeBW(doUpdate);
  247.         else
  248.             DrawCubeColor(doUpdate);
  249.         
  250.         if (CheckForWin())
  251.             WinGame();
  252.     }
  253. }
  254.  
  255. void ShutDownGame(void)
  256. {
  257.     int            i;
  258.     
  259.     ReleaseResource(gCleanMirror);
  260.     ReleaseResource(gKeyPic);
  261.     
  262.     for (i=0; i<4; i++)
  263.     {
  264.         if (gHasColorQD)
  265.             DisposePixPat(gColorPatterns[i]);
  266.         ReleaseResource(gBWPatterns[i]);
  267.     }
  268. }
  269.